#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 设置各种图标的图片
    ui->pushButton_target->setIcon(QIcon(":/image/doge.png"));
    ui->pushButton_target->setIconSize(QSize(ui->pushButton_target->width(), ui->pushButton_target->height()));

    ui->pushButton_up->setIcon(QIcon(":/image/up.png"));
    ui->pushButton_up->setIconSize(QSize(60,60));

    ui->pushButton_down->setIcon(QIcon(":/image/down.png"));
    ui->pushButton_down->setIconSize(QSize(60,60));

    ui->pushButton_left->setIcon(QIcon(":/image/left.png"));
    ui->pushButton_left->setIconSize(QSize(ui->pushButton_left->width(), ui->pushButton_left->height()));

    ui->pushButton_right->setIcon(QIcon(":/image/right.png"));
    ui->pushButton_right->setIconSize(QSize(ui->pushButton_right->width(), ui->pushButton_right->height()));

    // 设置快捷键
    // 直接通过按键的名字来设置，虽然简单，但是容易出错，因为是我们自己检查
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));

    // 通过 QT 内置的按键枚举来设置快捷键，键盘的快捷键是默认支持连发的
    // 这种方式还可以使用组合键：
    // ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));

    // 鼠标是默认是单点的，开启鼠标的连点功能需要手动开启
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);

    // 还可以设置连点的延时时间:仅仅针对鼠标，对键盘不起作用（单位：ms）
    ui->pushButton_up->setAutoRepeatDelay(7000);  // 按住 7 秒后才起作用

    // 还可以设置连点重复触发的周期
    ui->pushButton_down->setAutoRepeatInterval(5000);  // 连点时每隔 5 秒才触发一次槽函数
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_target_clicked()
{

}

void Widget::on_pushButton_up_clicked()
{
    // 获取当前target的坐标位置
    QRect rect = ui->pushButton_target->geometry();
    // 更新当前坐标
    ui->pushButton_target->setGeometry(rect.x(), rect.y()-5, rect.width(), rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    // 获取当前target的坐标位置
    QRect rect = ui->pushButton_target->geometry();
    // 更新当前坐标
    ui->pushButton_target->setGeometry(rect.x()-5, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    // 获取当前target的坐标位置
    QRect rect = ui->pushButton_target->geometry();
    // 更新当前坐标
    ui->pushButton_target->setGeometry(rect.x(), rect.y()+5, rect.width(), rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    // 获取当前target的坐标位置
    QRect rect = ui->pushButton_target->geometry();
    // 更新当前坐标
    ui->pushButton_target->setGeometry(rect.x()+5, rect.y(), rect.width(), rect.height());
}
